package edu.berkeley.drivingbuddy;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendFriend extends Activity implements OnClickListener, OnLongClickListener{
    /** Called when the activity is first created. */
    Button mButtonSendFriendSend;
    EditText mContactNumber;
    EditText mMessage;
    private static final int PICK_CONTACT = 1010;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sendfriend);
        
        mButtonSendFriendSend = (Button) findViewById(R.id.button_sendfriends_send);
        mButtonSendFriendSend.setOnClickListener(this);
        
        mMessage = (EditText) findViewById(R.id.txtMessage);
        mMessage.setText("Check Out This Score! ");
        mContactNumber = (EditText) findViewById(R.id.contactPhoneNo);
        mContactNumber.setOnLongClickListener(this);
    }
    
    @Override
    public boolean onLongClick(View v){
        if(v.getId()==R.id.contactPhoneNo){
            Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
            startActivityForResult(intent, PICK_CONTACT);
            return true;
        }else return false;
    }//onLongClick
    
    @Override
    public void onClick(View v) {
        switch (v.getId()){
         
        case R.id.button_sendfriends_send:
            Toast.makeText(this, "Your Score Has Been Sent", Toast.LENGTH_LONG).show();
            finish();
            break;


        }//switch
    }//onClick
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent){   
        //may be error if there is no contacts
        if (resultCode == RESULT_OK){
            switch(requestCode){
            case PICK_CONTACT:
                Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
                cursor.moveToNext();
                String name = cursor.getString(cursor.getColumnIndexOrThrow
                        (ContactsContract.Contacts.DISPLAY_NAME));
                cursor = null;
                mContactNumber.setText(name); 
                break;
            }
        }else Toast.makeText(this, "Activity Result Error", Toast.LENGTH_LONG).show(); 
    }//onActivityResult
    
}